<template>
  <el-container>
    <el-header>
      <!-- 头部区 -->
      <img src="../assets/images/logo.png" alt="" />
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="rgb(64, 158, 255)"
      >
        <el-submenu index="1">
          <template slot="title">
            <img class="avatar" src="../assets/logo.png" alt="" />
            个人中心
          </template>
          <el-menu-item index="1-1"><i class="el-icon-key"></i>重置密码</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-s-operation"></i>用户资料</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="logout"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <div class="user-info">
          <img class="avatar" v-if="user.user_pic" :src="user.user_pic" alt="" />
          <img class="avatar" v-else src="@/assets/logo.png" alt="" />
          <p>
            欢迎你 <span>{{ user.nickname || user.username }}</span>
          </p>
        </div>
        <el-menu
          mode="vertical"
          background-color="#23262e"
          text-color="#fff"
          active-text-color="rgb(64, 158, 255)"
          default-active="1"
          unique-opened
          router
        >
          <template v-for="item in menu">
            <el-menu-item
              v-if="item.children === null"
              :index="item.indexPath"
              :key="item.indexPath"
              ><i :class="item.icon"></i>{{ item.title }}</el-menu-item
            >
            <el-submenu :index="item.indexPath" v-else :key="item.indexPath">
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item
                :index="subItem.indexPath"
                v-for="subItem in item.children"
                :key="subItem.indexPath"
                ><i :class="subItem.icon"></i>{{ subItem.title }}</el-menu-item
              >
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <!-- 内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  created() {
    // 利用生命周期函数自动调用的特点，调用vuex中的actons方法
    this.$store.dispatch('user/getUser')
    this.getMenu()
  },
  computed: {
    ...mapState('user', ['user'])
  },
  methods: {
    // 退出方法
    logout() {
      // 询问是否确定退出，清空token，和用户信息
      this.$confirm('你确定要退出吗?', '提示', {
        confirmButtonText: '确定', // 配置按钮文字
        cancelButtonText: '取消',
        type: 'warning' // 弹框类型：警告框
      })
        .then(() => {
          // 点击确定
          this.$store.commit('user/updateToken', '')
          this.$store.commit('user/updaateUser', {})
          this.$router.push('/login')
          this.$message({
            type: 'success',
            message: '退出成功!'
          })
        })
        .catch(() => {
          // 点击取消
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    },
    // 获取菜单数据
    async getMenu() {
      // 调用API,获取数据
      const { data: res } = await getMenuAPI()
      // 将数据存储到当前数据的数据项中
      if (res.code === 0) {
        this.menu = res.data
      }
    }
  }
}
</script>

<style lang="less" scoped>
// 头部
.el-header {
  height: 70px;
  background-color: #23262e;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.avatar {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  object-fit: cover;
  background-color: #fff;
  margin-right: 13px;
}

// 侧边栏
.el-aside {
  height: 100%;
  background-color: #23262e;
  .el-menu {
    width: 200px;
    overflow: hidden;
  }
}

.el-menu-item {
  font-size: 13px;
}

// 无法修改子组件内部的样式，可以使用样式穿透
.el-submenu {
  /deep/ .el-submenu__title {
    font-size: 13px;
  }
}

// 内容区
.el-main {
  background-color: #f2f2f2;
}

// 最大的容器
.el-container {
  height: 100%;
}

.user-info {
  height: 65px;
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  p {
    color: #fff;
    font-size: 13px;
  }
}
</style>
